﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio.Datum
{
    /// <summary>
    /// 存储文件基类
    /// </summary>
    public class StorageXmlFile
    {
        /// <summary>
        /// XML操作类
        /// </summary>
        protected XmlReadWrite XmlHelperService { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 构造函数[受保护]
        /// </summary>
        public StorageXmlFile(string argName)
        {
            this.Name = argName;
            this.XmlHelperService = new XmlReadWrite();
        }

        /// <summary>
        /// 写入XML文件数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argXMLData">XML存储对象</param>
        /// <param name="argFileFullPathName">文件绝对全路径名称（可带后缀）</param>
        /// <returns></returns>
        public Result XMLWriteData<T>(T argXMLData, string argFileFullPathName) where T : class, new()
        {
            Result ResultData = new Result() { ResultCode = ResultStatus.Failure };
            if (argXMLData == null) { return ResultData; }
            try
            {
                //[对象] => [XML文件字符串]
                string strXMLReturn = argXMLData.XmlSerialize();
                if (!string.IsNullOrWhiteSpace(strXMLReturn))
                {
                    //[XML文件字符串]  => [XML文件]
                    string strResult = this.XmlHelperService.WriteXmlFile(strXMLReturn, argFileFullPathName);
                    if (strResult != null)
                    {
                        ResultData.ResultCode = ResultStatus.Failure;
                        ResultData.ResultMsg = strResult;
                    }
                    else { ResultData.ResultCode = ResultStatus.Succeed; }
                }
            }
            catch (Exception ep)
            {
                ResultData.ResultCode = ResultStatus.Exception;
                ResultData.ResultMsg = $"{this.Name} 保存XML文件[{argFileFullPathName}]异常：" + ep.Message;
            }
            return ResultData;
        }

        /// <summary>
        /// 写入XML文件数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argXMLData">XML存储对象</param>
        /// <param name="argFileFullPathName">文件绝对全路径名称（可带后缀）</param>
        /// <returns>状态结果对象</returns>
        public Result XMLWriteStreamData<T>(T argXMLData, string argFileFullPathName)
        {
            Result ResultData = new Result() { ResultCode = ResultStatus.Failure };
            if (argXMLData == null) { return ResultData; }
            try
            {
                //[对象] => [XML文件字符串] => [XML文件]
                string strResult = this.XmlHelperService.WriteXmlFileStream(argXMLData, argFileFullPathName);
                if (strResult != null)
                {
                    ResultData.ResultCode = ResultStatus.Failure;
                    ResultData.ResultMsg = strResult;
                }
                else { ResultData.ResultCode = ResultStatus.Succeed; }
            }
            catch (Exception ep)
            {
                ResultData.ResultCode = ResultStatus.Exception;
                ResultData.ResultMsg = $"{this.Name} 保存XML文件[{argFileFullPathName}]异常：" + ep.Message;
            }
            return ResultData;
        }

        /// <summary>
        /// 读取XML文件数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argFileFullPathName">文件绝对全路径名称（可带后缀）</param>
        /// <returns>数据模型对象</returns>
        public ResultModel<T> XMLReadData<T>(string argFileFullPathName) where T : class, new()
        {
            ResultModel<T> ResultData = new ResultModel<T>();
            try
            {
                string strReadXmlFileMsg = string.Empty;
                //[XML文件]  => [XML文件字符串]
                string strXMLData = this.XmlHelperService.ReadXmlFile(argFileFullPathName, ref strReadXmlFileMsg);
                if (!string.IsNullOrWhiteSpace(strXMLData))
                {
                    //[XML文件字符串] => [对象]
                    T? XMLReturnData = strXMLData.XmlDeserialize<T>();
                    ResultData.ResultCode = ResultStatus.Succeed;
                    ResultData.Data = XMLReturnData;
                }
                else
                {
                    ResultData.ResultCode = ResultStatus.Failure;
                    ResultData.ResultMsg = strReadXmlFileMsg;
                }
            }
            catch (Exception ep)
            {
                ResultData.ResultCode = ResultStatus.Exception;
                ResultData.ResultMsg = $"{this.Name} 读取XML文件[{argFileFullPathName}]数据异常：" + ep.Message;
            }
            return ResultData;
        }

        public ResultModel<T> XMLReadStreamData<T>(string argFileFullPathName) where T : class, new()
        {
            //[XML文件]  => [XML文件字符串] => [对象]
            T? RetData = this.XmlHelperService.ReadXmlFileStream<T>(argFileFullPathName);
            return new ResultModel<T>()
            {
                Data = RetData,
                ResultCode = (RetData != null) ? ResultStatus.Succeed : ResultStatus.Failure,
                ResultMsg = (RetData != null) ? string.Empty : ("读取XML文件[" + this.Name + "]数据失败")
            };
        }
    }
}
